package com.ftg.learn.controller;

import dev.langchain4j.data.message.ImageContent;
import dev.langchain4j.data.message.TextContent;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.response.ChatResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;

@RestController
public class ImgToTextController {


    @Autowired
    private ChatModel imageUnderstandingModel;

    @PostMapping("/understand")
    public String understandImage(@RequestParam("image") MultipartFile image,
                                  @RequestParam(value = "prompt", defaultValue = "请描述这张图片的内容") String prompt) throws Exception {
        // 将图片转换为Base64编码
        byte[] imageBytes = image.getBytes();
        String base64Image = Base64.getEncoder().encodeToString(imageBytes);

        // 构造包含文本和图像的消息
        UserMessage userMessage = UserMessage.from(
                TextContent.from(prompt),
                ImageContent.from(base64Image, image.getContentType())
        );

        // 调用模型进行图像理解
        ChatResponse response = imageUnderstandingModel.chat(userMessage);
        return response.aiMessage().text();
    }
}


